<script setup lang="ts">
import { uploadImage } from "@/services/consult";
import type { Image } from "@/types/consult";
import { showLoadingToast } from "vant";
import type { UploaderAfterRead } from "vant/lib/uploader/types";
import { ref } from "vue";
defineProps<{
  disabled: boolean;
}>();

const emit = defineEmits<{
  (e: "send-text", text: string): void;
  (e: "send-image", image: Image): void;
}>();

const text = ref("");
const sendText = () => {
  emit("send-text", text.value);
  text.value = "";
};

// 提交图片
const sendImage: UploaderAfterRead = async (item) => {
  if (Array.isArray(item)) return;
  if (!item.file) return;
  const t = showLoadingToast({ message: "正在上传", duration: 0 });
  const res = await uploadImage(item.file);
  t.close();
  emit("send-image", res.data);
};
</script>

<template>
  <div class="room-action-page">
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      :disabled="disabled"
      v-model="text"
      @keyup.enter="sendText"
    />
    <van-uploader
      :after-read="sendImage"
      :preview-image="false"
      :disabled="disabled"
    >
      <van-icon name="photo-o" size="30"></van-icon>
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action-page {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #fff;
  height: 50px;
  .van-field {
    width: 300px;
    padding: 6px 10px;
    border-radius: 20px;
    background-color: #f6f7f9;
  }
}
</style>
